package com.woniu.his.cms.app.apiImpl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.cms.client.NonDrugClient;
import com.woniu.his.cms.client.config.OssUtil;
import com.woniu.his.cms.client.param.DmsNonDrugParam;
import com.woniu.his.cms.client.param.DmsNonDrugQueryParam;
import com.woniu.his.cms.client.param.DmsNonDrugUpdateParam;
import com.woniu.his.cms.client.param.UploadCheckResultParam;
import com.woniu.his.cms.client.result.*;
import com.woniu.his.cms.infra.model.NonDrug;
import com.woniu.his.cms.common.CmsResultCode;
import com.woniu.his.cms.infra.model.NonDrugItemRecord;
import com.woniu.his.cms.service.NonDrugItemRecordService;
import com.woniu.his.cms.service.NonDrugService;
import com.woniu.his.cms.service.dto.*;
import com.woniu.his.cms.service.param.DmsNonDrugSvParam;
import com.woniu.his.cms.service.param.NonDrugQuerySvParam;
import com.woniu.his.cms.service.param.NonDrugUpdateSvParam;
import com.woniu.his.cms.service.param.UploadCheckSvParam;
import com.woniu.his.exception.HisException;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/cms")
public class NonDrugApiImpl implements NonDrugClient {

    @Resource
    private NonDrugService nonDrugService;

    @Resource
    private NonDrugItemRecordService nonDrugItemRecordService;


    //新建检查处置项目
    @Override
    public Result<CreateNonDrugResult> createNonDrug(DmsNonDrugParam dmsNonDrugParam) {

        log.info("新建检查项目信息:{}", dmsNonDrugParam);

        DmsNonDrugSvParam dmsNonDrugSvParam = BeanUtil.copyProperties(dmsNonDrugParam, DmsNonDrugSvParam.class);
        CreateNonDrugDto createNonDrugDto = nonDrugService.create(dmsNonDrugSvParam);
        CreateNonDrugResult result = BeanUtil.copyProperties(createNonDrugDto, CreateNonDrugResult.class);
        return Result.getInstance(CreateNonDrugResult.class).setData(result).setMsg("新增非药品项目成功");
    }

    //根据ids删除非药品
    @Override
    public Result deleteNonDrug(List<Long> ids) {

        log.info("新建检查项目信息:{}", ids);
        boolean flag = nonDrugService.delete(ids);
        return flag ? Result.getInstance().setMsg("删除检查/处置项目成功") :
                Result.getInstance().setMsg("删除检查/处置项目失败");

    }


    //更新非药品项目
    @Override
    public Result<CreateNonDrugResult> updateNonDrug(@RequestBody DmsNonDrugUpdateParam dmsNonDrugUpdateParam) {

        log.info("更新检查项目信息:{}", dmsNonDrugUpdateParam);

        NonDrugUpdateSvParam nonDrugUpdateSvParam = BeanUtil.copyProperties(dmsNonDrugUpdateParam, NonDrugUpdateSvParam.class);
        UpdateDrugDto updateDrugDto = nonDrugService.updateNonDrug(nonDrugUpdateSvParam);
        CreateNonDrugResult result = BeanUtil.copyProperties(updateDrugDto, CreateNonDrugResult.class);
        return Result.getInstance(CreateNonDrugResult.class).setData(result).setMsg("修改非药品项目成功");
    }

    //查询所有非药品
    @Override
    public Result<List<DmsNonDrugResult>> listAllNonDrug() {
        List<DmsNonDrugDto> listResult = nonDrugService.listAll();
        List<DmsNonDrugResult> ts = BeanUtil.copyToList(listResult, DmsNonDrugResult.class);
        return Result.getInstanceList(DmsNonDrugResult.class).setData(ts).setMsg("查询非药品项目成功");
    }

    //模糊查询非药品、且分页
    @Override
    public Result<NonDrugQueryResult> queryList(DmsNonDrugQueryParam queryParam) {

        log.info("检查/处置模糊查询:{}", queryParam);

        NonDrugQuerySvParam querySvParam = BeanUtil.copyProperties(queryParam, NonDrugQuerySvParam.class);
        NonDrugQueryDto nonDrugQueryDto = nonDrugService.queryList(querySvParam);
        NonDrugQueryResult result = BeanUtil.copyProperties(nonDrugQueryDto, NonDrugQueryResult.class);
        if (nonDrugQueryDto!=null) {
            return    Result.getInstance(NonDrugQueryResult.class).setData(result).setMsg("检查/处置项目查询成功");
        }
        return  Result.getInstance(NonDrugQueryResult.class).setMsg("没有符合条件的检查/处置项目");
    }


    /**
     * 描述：根据科室id刷新患者待检查列表
     */
    @Override
    public Result<List<DmsMechanicItemRecordResult>> listByDept(Long deptId) {

        log.info("检查科室id:{}", deptId);
        List<DmsMechanicItemRecordDto> itemRecordDto = nonDrugService.listByDept(deptId);
        List<DmsMechanicItemRecordResult> result = BeanUtil.copyToList(itemRecordDto, DmsMechanicItemRecordResult.class);
        return Result.getInstanceList(DmsMechanicItemRecordResult.class).setData(result).setMsg("查询待检查病人成功");
    }


    /**
     * 描述：医技登记
     */
    @Override
    public Result logNonDrugItem(Long itemRecordId, Long logStaffId) {
        log.info("条目id :{}",   itemRecordId);
        log.info(" 登记人员id:{}", logStaffId);
        boolean b = nonDrugService.logNonDrugItem(itemRecordId, logStaffId);
        return b ? Result.getInstance().setMsg("患者检查登记成功") :
                Result.getInstance().setMsg("患者检查登记失败");
    }


    /**
     * 描述：上传结果
     */
    @Override
    public Result uploadResult(UploadCheckResultParam uploadCheckResultParam) {

        log.info("上传结果信息:{}", uploadCheckResultParam);

        UploadCheckSvParam param = BeanUtil.copyProperties(uploadCheckResultParam, UploadCheckSvParam.class);
        boolean b = nonDrugService.uploadResult(param);
        return b ? Result.getInstance().setMsg("患者检查信息上传成功") :
                Result.getInstance().setMsg("患者检查信息上传失败");

    }

    @Override
    public Result<List<NonDrugItemRecordQuerResult>> queryResultByCaseId(Long caseHistoryId) {
        QueryWrapper<NonDrugItemRecord> mapper = new QueryWrapper<NonDrugItemRecord>().eq("case_history_id", caseHistoryId);
        List<NonDrugItemRecord> list = nonDrugItemRecordService.list(mapper);
        if (list.isEmpty()) {
            throw new HisException(CmsResultCode.NON_DRUG_ITEM_NOT_EXITS);
        }
        List<NonDrugItemRecordQuerResult> results = BeanUtil.copyToList(list, NonDrugItemRecordQuerResult.class);
        for (NonDrugItemRecordQuerResult result : results) {
            if (result.getNonDrugItemStatus().equals("FINISH")) {
                result.setResultState("检查结果已出");
            }else {
                result.setResultState("检查结果未出");
            }
        }
        return Result.getInstanceList(NonDrugItemRecordQuerResult.class).setMsg("检查情况已查询").setData(results);
    }

    @Override
    public Result<DmsNonDrugResult> queryById(Long id) {
     log.info("根据id查询非药品:{}",id);
        NonDrug byId = nonDrugService.getById(id);
        if(byId == null) return null;
        if(byId.getNonDrugStatus().equals("OFF")) return null;
        return Result.getInstance(DmsNonDrugResult.class).setData(BeanUtil.copyProperties(byId,DmsNonDrugResult.class));
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public Result<String> upload(@NotNull MultipartFile file) throws Exception {

        log.info("开始上传图片名称:{}",file.getName());

        String fileName = file.getOriginalFilename();
        String type = fileName.substring(fileName.indexOf("."));
        String name = System.currentTimeMillis() + type;
        String path = OssUtil.uploadFile("woniudx", name, file.getInputStream());
        System.out.println("上传路径结果：" + path);
        return Result.getInstance(String.class).setMsg("上传成功").setData(path);
    }
}
